package common

import (
	"strings"
	"time"

	uuid "github.com/satori/go.uuid"
)

func times(str string, n int) (out string) {
	for i := 0; i < n; i++ {
		out += str
	}
	return
}

// Padleft left-pads the string with pad up to len runes
// len may be exceeded if
func Padleft(str string, length int, pad string) string {
	return times(pad, length-len(str)) + str
}

// PadRight right-pads the string with pad up to len runes
func PadRight(str string, length int, pad string) string {
	return str + times(pad, length-len(str))
}

// UUID 返回一个 unique ID
func UUID() string {
	return strings.Join(strings.Split(uuid.NewV4().String(), "-"), "") + Now()
}

// Now 当前时间
func Now() string {
	t := time.Now()
	ymd := Padleft(string(t.Year()), 4, "0") + Padleft(string(t.Month()), 2, "0") + Padleft(string(t.Day()), 2, "0")
	hms := Padleft(string(t.Hour()), 4, "0") + Padleft(string(t.Minute()), 2, "0") + Padleft(string(t.Second()), 2, "0")
	return ymd + "-" + hms
}
